'use strict';


// Declare app level module which depends on filters, and services
angular.module('myApp', [
  'ngRoute',
  'myApp.services',
  'myApp.filters',
  'myApp.directives',
  'myApp.controllersMain',
  'myApp.controllersGame'
])
.config(['$routeProvider', function($routeProvider) {
    $routeProvider.when('/main',            {templateUrl: 'partials/main.html', controller: 'MainCtrl'});
    $routeProvider.when('/main/about',      {templateUrl: 'partials/main_about.html', controller: 'MainAboutCtrl'});
    $routeProvider.when('/main/setting',    {templateUrl: 'partials/main_setting.html', controller: 'MainSettingCtrl'});
    $routeProvider.when('/main/stats',      {templateUrl: 'partials/main_stats.html', controller: 'MainStatsCtrl'});
    $routeProvider.when('/game/single',     {templateUrl: 'partials/game.html', controller: 'GameSingleCtrl'});
    $routeProvider.when('/game/multi',      {templateUrl: 'partials/game.html', controller: 'GameMultiCtrl'});
    $routeProvider.when('/game/quit',       {templateUrl: 'partials/game_quit.html', controller: 'GameQuitCtrl'});
    $routeProvider.otherwise({redirectTo:   '/main'});
}])
.run(['$location', '$rootScope', 'serviceGame', 'servicePopupFunc',
    function ($location, $rootScope, serviceGame, servicePopupFunc) {
        //$location.path('/');
        
        var popupShow = null;
        
        // back button
        document.addEventListener("backbutton", function(e) {
            
            e.preventDefault();
            
            if (serviceGame.winnerValue) {
                
                /**
                 * jest juz koniec gry
                 * przekierowanie na game/quit
                 */
                console.log('back button >> game is ended');
                
                if ($('#popupEndGameDialog').css('display') === 'block') {
                    $('#popupEndGameDialog').modal('hide');
                }
                
                $location.path('/game/quit');
                $rootScope.$apply();
                return;
            }
            
            
            if (popupShow !== null) {
                
                /**
                 * potwierdzenie wyjscia,
                 * symulacja eventu clik na popupie
                 */
                                
                if (popupShow.css('display') === 'block') {
                    console.log('back button >> game in progress 2');
                    
                    $location.path('/game/quit');
                    $rootScope.$apply();
                    popupShow.modal('hide');
                    
                    popupShow = null;
                    return;
                } else {
                    popupShow = null;
                }
            }
            
            if (serviceGame.nextMoveValue !== null) {
                
                /**
                 * prosba o potwierdzenie wyjscia
                 * otwarcie popupu
                 */
                
                console.log('back button >> game in progress');
                               
                if (serviceGame.singleOrMulti == 'multi') {
                    popupShow = servicePopupFunc.showPopupMultiBack();
                } else {
                    popupShow = servicePopupFunc.showPopupSingleBack();
                }
                return;
            } 
            
                
            /**
             * uzytkownik nie jest w grze
             * wiec nic sie nie dzieje
             */

            console.log('back button >> normal');
            popupShow = null;

            if ($location.path() === '/main') {
                console.log('exit application');
                navigator.app.exitApp();
            } else {
                
                /**
                 * zamykanie wszystkich innych popupow
                 * ktore moga byc otwarte
                 */
                if ($('#popupResetStatConfirm').css('display') === 'block') {
                    $('#popupResetStatConfirm').modal('hide');
                } else if ($('#popupEndGameDialog').css('display') === 'block') {
                    $('#popupEndGameDialog').modal('hide');
                } else {
                    window.history.back();
                }
            }
            
        }, true);

        
        var event = document.createEvent('Event');
        event.initEvent('appready', true, true);
        document.dispatchEvent(event);

    }
]);
